﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Infrastructure.Persistence.Converters
{
    public class ListOfIdsConverter : ValueConverter<List<Guid>, string>
    {
        public ListOfIdsConverter(
            ConverterMappingHints? mappingHints = null)
            : base(
                  v => string.Join(',', v),
                  v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(Guid.Parse).ToList(),
                  mappingHints)
        {
        }
    }

    public class ListOfIdsComparer : ValueComparer<List<Guid>>
    {
        public ListOfIdsComparer() : base(
          (t1, t2) => t1!.SequenceEqual(t2!),
          t => t.Select(x => x!.GetHashCode()).Aggregate((x, y) => x ^ y),
          t => t)
        {
        }
    }
}
